﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using BOHVote.Core;
using System.Data;

namespace BOHVote.DAL
{
    public class SysConfigProvide
    {
        Database db;
        public SysConfigProvide()
        {
            db = DatabaseFactory.CreateDatabase();
        }

        #region <<  Allow Modify Applyer by Unit and Govren  >>
        public string IsAllowModifyApplyer()
        {
            string strSQL = "SELECT Config_value FROM sys_config WHERE Config_class = 'Apply' AND config_name ='GovernModfiyApplyer' AND Config_status = 'A'";
            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);

            return (string)Utility.ExecuteScalar(db, dbcommand);
        }

        public int setAllowModifyApplyer(string ConfigValue, string status)
        {
            string strSQL = "INSERT INTO sys_config(Config_class,config_name,Config_status,Config_value)VALUES('Apply','GovernModfiyApplyer','" + status + "','" + ConfigValue + "')";
            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);
            return Utility.ExecuteNonQuery(db, dbcommand);
        }

        public int UpdateAllowModifyApplyer(string ConfigValue)
        {
            string strSQL = "Update sys_config Set Config_value  = '" + ConfigValue + "' WHERE config_name='GovernModfiyApplyer' AND Config_class ='Apply' AND Config_Status='A' ";
            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);
            return Utility.ExecuteNonQuery(db, dbcommand);
        }

        #endregion

        #region <<  PPT  >>
        public string IsAllowUploadPPT()
        {
            string strSQL = "SELECT Config_value FROM sys_config WHERE Config_class = 'Apply' AND config_name ='PPTUpload' AND Config_status = 'A'";
            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);

            return (string)Utility.ExecuteScalar(db, dbcommand);
        }


        public int setAllowUploadPPT(string ConfigValue,string status)
        {
            string strSQL = "INSERT INTO sys_config(Config_class,config_name,Config_status,Config_value)VALUES('Apply','PPTUpload','" + status + "','" + ConfigValue + "')";
            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);
            return  Utility.ExecuteNonQuery(db, dbcommand);
        }

        public int UpdateAllowUploadPPT(string ConfigValue)
        {
            string strSQL = "Update sys_config Set Config_value  = '" + ConfigValue + "' WHERE config_name='PPTUpload' AND Config_class ='Apply' AND Config_Status='A' ";
            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);
            return Utility.ExecuteNonQuery(db, dbcommand);
        }

        public int InsertSwitchSubject(string strsubject, string IsOpen)
        {
            string strSQL = "INSERT INTO sys_config(config_class,config_name,config_value,config_status)VALUES('SwitchSubject','"+strsubject+"','"+IsOpen+"','A')";
            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);
            return Utility.ExecuteNonQuery(db, dbcommand);
        }

        public int UpdateSwitchSubject(string strSubject, string IsOpen)
        {
            string strSQL = "UPDATE sys_config SET config_value = '" + IsOpen + "' WHERE config_class ='SwitchSubject' AND config_name='" + strSubject + "'";
            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);
            return Utility.ExecuteNonQuery(db, dbcommand);
        }

        public string getSwitchSubject(string strSubject)
        {
            string strSQL = "SELECT config_value FROM sys_config WHERE config_class ='SwitchSubject' AND config_name='" + strSubject + "' ";
            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);
            return (string)Utility.ExecuteScalar(db, dbcommand);
        }

        public DataSet getSwitchSubjects()
        {
            string strSQL = "SELECT s.SubjectID,s.SubjectName, sc.* FROM [subject] s left join sys_config sc ON s.SubjectID = sc.config_name WHERE sc.config_class ='SwitchSubject' ";
            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);
            return Utility.ExecuteDataSet(db, dbcommand);
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <returns>0:false;1:true; 0:dosen;t show person login select; 1:show.</returns>
        public string IsShowPersonLogin()
        { 
            string returnValue =string.Empty;

            string strSQL = "SELECT Config_value FROM sys_config WHERE Config_class = 'sys' AND config_name ='PersonLogin' AND Config_status = 'A'";

            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);

            returnValue = (string)Utility.ExecuteScalar(db, dbcommand);

            return returnValue;
        }

        public int setIsShowPersonLogin(string ConfigValue,string status)
        {
            string strSQL = "INSERT INTO sys_config(Config_class,config_name,Config_status,Config_value)VALUES('sys','PersonLogin','" + status + "','" + ConfigValue + "')";
            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);
            return  Utility.ExecuteNonQuery(db, dbcommand);
        }

        public int UpdateIsShowPersonLogin(string ConfigValue)
        {
            string strSQL = "Update sys_config Set Config_value  = '" + ConfigValue + "' WHERE config_name='PersonLogin' AND Config_class ='sys' AND Config_Status='A' ";
            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);
            return Utility.ExecuteNonQuery(db, dbcommand);
        }

        public int SetConfigApplyStyle(string ConfigValue, string status)
        {
            string strSQL = "INSERT INTO sys_config(Config_class,config_name,Config_status,Config_value)VALUES('Apply','SwitchApplyStyle','" + status + "','" + ConfigValue + "')";
            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);
            return Utility.ExecuteNonQuery(db, dbcommand);
        }

        public int UpdateConfigApplyStyle(string ConfigValue)
        {
            string strSQL = "Update sys_config Set Config_value  = '" + ConfigValue + "' WHERE config_name='SwitchApplyStyle' AND Config_class ='Apply' AND Config_Status='A' ";
            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);
            return Utility.ExecuteNonQuery(db, dbcommand);
        }

        public string GetConfigApplyStyle()
        {
            string returnValue = string.Empty;

            string strSQL = "SELECT Config_value FROM sys_config WHERE Config_class = 'Apply' AND config_name ='SwitchApplyStyle' AND Config_status = 'A'";

            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);

            returnValue = (string)Utility.ExecuteScalar(db, dbcommand);

            return returnValue;
        }


        #region <<  Test Module  >>
        public DataSet getAllSubjectTestModule(string status,string subjectID)
        {
            string strSQL = "SELECT tm.*,s.SubjectName,s.OrderID FROM TestModule tm left join Subject s on tm.SubjectID = s.SubjectID WHERE 1=1 ";
            if (!string.IsNullOrEmpty(status))
                strSQL += " AND tm.STATUS='" + status + "'";
            if(!string.IsNullOrEmpty(subjectID))
                strSQL += " AND tm.SubjectID='" + subjectID + "'";
            strSQL += "  order by s.OrderID";
            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);
            return Utility.ExecuteDataSet(db, dbcommand);
        }

        public DataSet getAllASubjectTestModule()
        {
            return getAllSubjectTestModule("A", string.Empty);
        }

        public DataSet getSubjectTestModuleByID(string ID)
        {
            string strSQL = "SELECT * FROM TestModule WHERE 1=1 AND SN='" + ID + "'";
            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);
            return Utility.ExecuteDataSet(db, dbcommand);
        }

        public int UpdateSubjectTestModule(string ID,string testModule)
        {
            string strSQL = "UPDATE TestModule SET TestModule = '" + testModule + "' WHERE SN=" + ID ;
            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);
            return Utility.ExecuteNonQuery(db, dbcommand);
        }

        public int InsertSubjectTestModule(string subjectID, string testModule)
        {
            string strSQL = "Insert INTO TestModule(SubjectID,TestModule,Status)VALUES('"+subjectID+"','"+testModule+"','A')";
            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);
            return Utility.ExecuteNonQuery(db, dbcommand);
        }

        public int DeleteSubjectTestModule(string ID)
        {
            string strSQL = "DELETE FROM testModule where SN="+ID;
            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);
            return Utility.ExecuteNonQuery(db, dbcommand);
        }

        public string GetTestModuleBySubjectID(string subjectID)
        {
            string strSQL = "SELECT TestModule from TestModule where SubjectID='" + subjectID + "'";
            DbCommand dbcommand = db.GetSqlStringCommand(strSQL);
            return (string)Utility.ExecuteScalar(db, dbcommand);
        }
        #endregion
    }
}
